package yxy.game.pm2.command.hero;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.PackageHandle;

/** 武将装备 武将换装获取装备数据 */
@MessageType(proto = Command.HeroEquip, classtype = HeroMessage.HeroEquipRequest.class)
public class CMD_HeroEquip extends Command<HeroMessage.HeroEquipRequest> {

	@Override
	public void execute(MessageSource messageSource, HeroMessage.HeroEquipRequest request) {
		String playerCode = messageSource.getTag();
		int equippos = request.getEquippos();

		HeroMessage.HeroEquipResponse.Builder response = HeroMessage.HeroEquipResponse.newBuilder();

		try {
			PackageMessage.Package.Builder pack = new PackageHandle().getEquipPackageByPos(playerCode, equippos);
			response.setPack(pack);
			send(messageSource, response, Result.Success);
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, HeroMessage.HeroEquipResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}